***PREPARED MARCH 2017 AFTER ACCEPTANCE TO WORLD DEVELOPMENT

***1)Defining directories (for mac/windows/computer switching) and loading data
drop _all
set more off
*Price Discrim Project, 2nd Wave
****Built off 2nd wave analysis, incorporating R&R and final for WD 2016 (V7)
*****Revised following additional comments March 2017

global home2 /Users/danhonig/Dropbox/Price Discrimination in Developing Markets

*Loading the CSV
*insheet using "$home/Dropbox/Price Discrimination in Developing Markets/Second Audit/DATA.5.26.15 - uncert wrong seller.csv"
insheet using "$home2/Second Audit/DATA.5.26.15 - uncert wrong seller.csv"
cd "$home2/Version 9 accepted"
save "Final 2nd wave data 2015.dta", replace


****2) Coding and Cleaning


*Coding Unkempt/Radiant
replace class="1" if class=="R"
replace class="0" if class=="U"
destring class, replace
*label define radiant 1 "Radiant" 0 "Unkempt"
label values class radiant
rename class confed_radiant

**Encode rice seller ID

encode (ricesellerid), gen(agent_id)

**Rename match

rename ethnicitymatch sameethnic



*Coding confederateethnicity Yoruba/Igbo
replace confederateethnicity="1" if confederateethnicity=="y"
replace confederateethnicity="0" if confederateethnicity=="i"
destring confederateethnicity, replace
*label define ethnicity 1 "Yoruba" 0 "Igbo"
label values confederateethnicity ethnicity

*Coding ricesellerethnicity Yoruba/Igbo
replace ricesellerethnicity="1" if ricesellerethnicity=="y"
replace ricesellerethnicity="0" if ricesellerethnicity=="I"
destring ricesellerethnicity, replace
label values ricesellerethnicity ethnicity

**Destring price 
gen refused=1 if price=="didn't sell to him, didn't want to sell one dereka"
replace price="" if price=="didn't sell to him, didn't want to sell one dereka"
destring price, replace

**Calculating 'discrimination'
sort agent_id
by agent_id: egen agentmaxprice=max(price)
by agent_id: egen agentminweight=min(weight)


order price weight confed_radiant agent_id confederateid

**Fill in refused

replace price=1.25*agentmaxprice if refused==1
replace weight=.75*agentminweight if refused==1

**Renaming "price"

rename price total_cost


**Generating price per unit ratio to use as outcome

gen Pricepergram=total_cost/weight
gen Priceperkilo=total_cost/weight*1000

*Generating confedID dummies for confed FE
rename confederateid confed_id
tabulate confed_id, gen (confeddum)

*Generating agentID dummies for agent FE
tabulate agent_id, gen (agentdum)

*Generating interaction terms - radiant (high class) and ethnic/religion
gen confed_radXsameethnic=confed_radiant*sameethnic

*Generating dummies for ethnicity
tabulate confederateethnicity, gen (ethnicdum)

**Drop some of the troubling uncertainties coded in the data


drop if uncertaintyother==1
drop if uncertaintydropweight==1
drop if uncertaintyleaking==1
drop if uncertaintychild==1

**3) Analysis

***Labeling Variables
label variable confed_radiant "High SES"
label variable sameethnic "Ethnic Match"
label variable confed_radXsameethnic "HighSES*Ethnic Match"
label variable total_cost "Total Price (Naira)"
label variable weight "Weight (g)"
label variable Pricepergram "Price (Naira) per gram"
label variable Priceperkilo "Price (Naira) per kg"


***Summary statistics table (THIS CREATES TABLE 2)
eststo clear
estpost summarize  total_cost weight Pricepergram sameethnic confed_radiant
esttab using 2ndwaveWDRRsum.tex, label replace cells ("count mean sd min max")

***Figure 1 of weight and price as in paper generated from R, but approximated by code below

hist weight
hist total_cost


**Main analysis table (THIS CREATES TABLE 3)

eststo clear
eststo: cluster2 Priceperkilo sameethnic agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
eststo: cluster2 Priceperkilo confed_radiant agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
eststo: cluster2 Priceperkilo sameethnic confed_radiant agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
eststo: cluster2 Priceperkilo sameethnic confed_radiant confed_radXsameethnic agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
esttab using WDrevision8maintable.tex, se label r2 obslast replace booktabs drop (agentdum* confeddum*) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)

**Doing margins, marginsplot (Basis of Table 4)
reg Priceperkilo i.sameethnic##i.confed_radiant i.agent_id i.confed_id, vce (cluster agent_id)
margins  i.sameethnic#i.confed_radiant
margins  i.sameethnic#i.confed_radiant, pwcompare level(90)
margins  i.sameethnic#i.confed_radiant, pwcompare level(99)
margins  i.sameethnic#i.confed_radiant, pwcompare level(95)


**Mechanisms Tables (Price, Weight; Tables 5 and 6)
eststo clear
eststo: cluster2 total_cost sameethnic  agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
eststo: cluster2 total_cost confed_radiant agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
eststo: cluster2 total_cost sameethnic confed_radiant agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
eststo: cluster2 total_cost sameethnic confed_radiant confed_radXsameethnic agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
esttab using WDrevisionprice.tex, se label r2 obslast replace booktabs drop (agentdum* confeddum*) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)

eststo clear
eststo: cluster2 weight sameethnic agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
eststo: cluster2 weight confed_radiant agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
eststo: cluster2 weight sameethnic confed_radiant agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
eststo: cluster2 weight sameethnic confed_radiant confed_radXsameethnic agentdum1-agentdum36 confeddum1-confeddum20, fcluster(agent_id) tcluster(confed_id)
esttab using WDrevisionweight.tex, se label r2 obslast replace booktabs drop (agentdum* confeddum*) star(+ 0.10 * 0.05 ** 0.01 *** 0.001)


***4) Responding to Reviewer Comments, World Development

*Establishing the number of sellers of each ethnicity/proportion of interactions
tab agent_id ricesellerethnicity

**Checking that the effect of coethnicity does not vary by seller...

*...by splitting sample
reg Pricepergram sameethnic confed_radiant confed_radXsameethnic agentdum1-agentdum36 confeddum1-confeddum20, vce (cluster agent_id)
reg Pricepergram sameethnic confed_radiant confed_radXsameethnic agentdum1-agentdum36 confeddum1-confeddum20 if ricesellerethnicity==1, vce (cluster agent_id)
reg Pricepergram sameethnic confed_radiant confed_radXsameethnic agentdum1-agentdum36 confeddum1-confeddum20 if ricesellerethnicity==0, vce (cluster agent_id)

*...by running a triple-interaction
reg Pricepergram sameethnic##confed_radiant##ricesellerethnicity agentdum1-agentdum36 confeddum1-confeddum20, vce (cluster agent_id)





